#-------------------------------------------------------------------------------
# SERMENT: Solving Eigenvalue Response Matrix Equations using Newton's Technique
#
# serment/CMakeLists.txt
#
# Note, this file was based in part on S. Johnson's PyTRT's CMake-based build
# system, which, like the rest of the code, is quite nicely written.
#
# $Rev:: 103                                           $:Rev of last commit
# $Author:: j.alyn.roberts@gma                         $:Author of last commit
# $Date:: 2011-06-14 22:22:46 -0400 (Tue, 14 Jun 2011) $:Date of last commit  
#
#-------------------------------------------------------------------------------

#-------- PROJECT -------------------------------------------------------------#

PROJECT( SERMENT )
CMAKE_MINIMUM_REQUIRED( VERSION 2.6 FATAL_ERROR )
SET(SERMENT_DIR ${CMAKE_CURRENT_SOURCE_DIR})

#-------- VERSION -------------------------------------------------------------#

SET( VERSION_MAJOR 0 )
SET( VERSION_MINOR 1 )
SET( VERSION_PATCH_LEVEL 0 )

MESSAGE( 
"++ SERMENT version ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH_LEVEL}" 
)

#-------- CMAKE MODULES -------------------------------------------------------#

# CMake files included with SERMENT
LIST( APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/tools/CMake" )

# CMake standard modules
# may need help


#--------- OPTIONS ------------------------------------------------------------#

# Set the default build type (debug)
IF( NOT CMAKE_BUILD_TYPE)
  SET( CMAKE_BUILD_TYPE Debug CACHE STRING
       "Choose the type of build, options are: Debug Release."
       FORCE)
ENDIF()

# Options for DBC macros.  For now, all on or all off.
SET( SERMENT_ENABLE_DBC YES CACHE BOOL
  "Enable design by contract macros."
)

IF(CMAKE_BUILD_TYPE STREQUAL "Release")
    IF(SERMENT_ENABLE_DBC)
        MESSAGE("WARNING: using DBC macros with Release configuration!")
    ENDIF()
ENDIF()

# Options for debug output. On or Off.
SET( SERMENT_ENABLE_DEBUG YES CACHE BOOL
  "Enable diagonostic print outs."
)

# Options for testing. On or Off.
SET( SERMENT_ENABLE_TEST YES CACHE BOOL
  "Enable testing."
)

# Options for building package _executables_
SET( SERMENT_BUILD_ERME YES CACHE BOOL
  "Build eigenvalue response matrix equation solver."
)
SET( SERMENT_BUILD_DIFF2D YES CACHE BOOL
  "Build two dimensional diffusion solver."
)
SET( SERMENT_BUILD_SLABTRAN YES CACHE BOOL
  "Build deterministic transport module."
)

# Options for using external libraries.

# PETSc is used in erme and diff2d and later in slabtran. 
SET(SERMENT_ENABLE_PETSC NO CACHE BOOL
    "Use PETSc solvers and datatypes."
)
IF( NOT SERMENT_ENABLE_PETSC )
  IF( SERMENT_BUILD_ERME OR SERMENT_BUILD_DIFF2D )
    MESSAGE( FATAL_ERROR "You must enable PETSc for ERME and DIFF2D" )
  ENDIF()
ENDIF()

# Silo can be used for flux outs and eventually more
SET( SERMENT_ENABLE_SILO YES CACHE BOOL
  "Use the Silo library for data i/o."
)

# XML is currently the only input option for erme and slabtran
SET( SERMENT_ENABLE_XML YES CACHE BOOL
  "Use the libxml2 library for input."
)

# MPI is currently not used anywhere, but could be later.
SET( SERMENT_ENABLE_MPI NO CACHE BOOL
  "Enable MPI."
)

# Threading is currently not used anywhere, but could be later.
SET( SERMENT_ENABLE_OPENMP NO CACHE BOOL
  "Enable OpenMP."
)

# Write config file, install it, and include that directory for all source
SET( SERMENT_CONFIG_IN  "${CMAKE_CURRENT_SOURCE_DIR}/serment_config.h.in" )
SET( SERMENT_CONFIG_OUT "${PROJECT_BINARY_DIR}/config/serment_config.h" )
message( "SERMENT_CONFIG_IN = ${SERMENT_CONFIG_IN}" )
CONFIGURE_FILE( ${SERMENT_CONFIG_IN} ${SERMENT_CONFIG_OUT} )
INCLUDE_DIRECTORIES( "${PROJECT_BINARY_DIR}/config" )


#-------- EXTERNAL LIBRARIES --------------------------------------------------#

MESSAGE(STATUS "Loading external libraries")

IF( SERMENT_ENABLE_PETSC )
    FIND_PACKAGE( PETSc REQUIRED )
    INCLUDE_DIRECTORIES( ${PETSC_INCLUDES} )
    ADD_DEFINITIONS( ${PETSC_DEFINITIONS} )
ENDIF()

IF( SERMENT_ENABLE_SILO )
    FIND_PACKAGE( Silo )
    IF( SILO_FOUND )
        INCLUDE_DIRECTORIES (${Silo_INCLUDE_DIR})
    ELSE()
        SET( SERMENT_ENABLE_SILO OFF )
        MESSAGE( STATUS "Disabling Silo since it was not found" )
    ENDIF()
ENDIF()

IF( SERMENT_ENABLE_XML )
    FIND_PACKAGE( LibXml2 REQUIRED )
    INCLUDE_DIRECTORIES ( ${LIBXML2_INCLUDE_DIR} )
    ADD_DEFINITIONS ( ${LIBXML2_DEFINITIONS} )
ENDIF()

IF( SERMENT_ENABLE_MPI )
  FIND_PACKAGE( MPI REQUIRED )
ENDIF()

IF( SERMENT_ENABLE_OPENMP)
  FIND_PACKAGE( OpenMP REQUIRED )
ENDIF()

#-------- TESTING ------------------------------------------------------#

OPTION ( USE_TESTING "Build testing" ON )
if ( SERMENT_ENABLE_TEST )
    SET ( CTEST_INITIAL_CACHE "
      COVERAGE_COMMAND:FILEPATH=/usr/bin/gcov
    " )
    # need for coverage -- probably a cleaner way to set this up.
    SET( CMAKE_CXX_FLAGS        
         "-g -O0  -fprofile-arcs -ftest-coverage" CACHE STRING "Choose"
    )
    SET( CMAKE_CXX_FLAGS        
         "-g -O0  -fprofile-arcs -ftest-coverage" CACHE STRING "Choose"
    )
    SET( CMAKE_C_FLAGS          
         "-g -O0  -fprofile-arcs -ftest-coverage" CACHE STRING "Choose"
    )
    SET( CMAKE_EXE_LINKER_FLAGS 
         "-g -O0  -fprofile-arcs -ftest-coverage" CACHE STRING "Choose"
    )
    SET( MEMORYCHECK_COMMAND 
       /home/robertsj/opt/valgrind/bin/valgrind CACHE STRING 
       "Set the memory checking command."
    )
    SET( MEMORYCHECK_COMMAND_OPTIONS 
       "--tool=memcheck --leak-check=full" CACHE STRING 
       "Set the memory checking command options."
    ) 
    ENABLE_TESTING()
    ADD_SUBDIRECTORY(test)
    INCLUDE(CTest)
endif()

#-------- SOURCE ------------------------------------------------------#
INCLUDE_DIRECTORIES( src )
ADD_SUBDIRECTORY( src )

#-------- OUTPUT ------------------------------------------------------#

message("++++ System:      ${CMAKE_SYSTEM} ${CMAKE_SYSTEM_PROCESSOR}")
message("++++ CXX:         ${CMAKE_CXX_COMPILER}")
message("++++ Build type:  ${CMAKE_BUILD_TYPE}")

if(SERMENT_ENABLE_DBC)
  message("++++++ DBC enabled")
else()
  message("++++++ DBC DISABLED")
endif()

if(SERMENT_ENABLE_PETSC)
  message("++++++ PETSc enabled")
else()
  message("++++++ PETSc DISABLED")
endif()
if(SERMENT_ENABLE_SILO)
  message("++++++ Silo enabled")
else()
  message("++++++ Silo DISABLED")
endif()
if(SERMENT_ENABLE_MPI)
  message("++++++ MPI enabled")
else()
  message("++++++ MPI DISABLED")
endif()
if(SERMENT_ENABLE_OPENMP)
  message("++++++ OpenMP enabled")
else()
  message("++++++ OpenMP DISABLED")
endif()

if(NOT SERMENT_BUILD_ERME)
  message("++++++ erme DISABLED")
endif()
if(NOT SERMENT_BUILD_DIFF2D)
  message("++++++ diff2d DISABLED")
endif()
if(NOT SERMENT_BUILD_SLABTRAN)
  message("++++++ slabtran DISABLED")
endif()



